package loja.view.editor.components.comboboxmodel;

import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

public class EntityComboBoxModel<E> extends AbstractListModel<E> implements
		ComboBoxModel<E> {

	private static final long serialVersionUID = 1L;

	private List<E> entities;
	private Object selectedObject;

	public EntityComboBoxModel(List<E> entities) {
		this.entities = entities;
	}

	@Override
	public int getSize() {
		return entities.size();
	}

	@Override
	public E getElementAt(int index) {
		return entities.get(index);
	}

	@Override
	public void setSelectedItem(Object anItem) {
		if ((selectedObject != null && !selectedObject.equals(anItem))
				|| selectedObject == null && anItem != null) {
			selectedObject = anItem;
		}
	}

	@Override
	public Object getSelectedItem() {
		return selectedObject;
	}

}
